package com.yikaiye.android.yikaiye.ui.activity.home.viewmodel;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.di.retrofit.BaseModel;
import com.yikaiye.android.yikaiye.di.retrofit.BaseNetCallback;
import com.yikaiye.android.yikaiye.model.AlipayModel;
import com.yikaiye.android.yikaiye.model.CommitOrderResultModel;
import com.yikaiye.android.yikaiye.model.CompanyRecordsModel;
import com.yikaiye.android.yikaiye.model.OrderProductModel;
import com.yikaiye.android.yikaiye.pay.WeChatInfoModel;
import com.yikaiye.android.yikaiye.ui.fragment.viewmodel.OrderStatusViewModel;
import com.yikaiye.android.yikaiye.utils.ToastHelper;

import java.util.HashMap;
import java.util.Map;

public class ConfirmOrderViewModel extends OrderStatusViewModel {

    /**
     * 我的企业
     */
    public MutableLiveData<CompanyRecordsModel> companyRecordsModelMutableLiveData = new MutableLiveData<>();
    /**
     * 订单提交成功
     */
    public MutableLiveData<CommitOrderResultModel> commitOrderSuccessMutableLiveData = new MutableLiveData<>();
    /**
     * 订单提交失败
     */
    public MutableLiveData<String> commitOrderErrorMutableLiveData = new MutableLiveData<>();
    /**
     * 微信支付成功
     */
    public MutableLiveData<WeChatInfoModel> wechatPaySuccessMutableLiveData = new MutableLiveData<>();
    /**
     * 微信支付失败
     */
    public MutableLiveData<String> wechatPayErrorMutableLiveData = new MutableLiveData<>();
    /**
     * 支付宝支付成功
     */
    public MutableLiveData<AlipayModel> aliPaySuccessMutableLiveData = new MutableLiveData<>();
    /**
     * 支付宝支付失败
     */
    public MutableLiveData<String> aliPayErrorMutableLiveData = new MutableLiveData<>();
    /**
     * 订单详情
     */
    public MutableLiveData<OrderProductModel> orderDetailsMutableLiveData = new MutableLiveData<>();

    /**
     * 提交订单
     */
    public void commitOrder(String json) {
        addSubscribe(mRetrofitHelper.commitOrder(json).subscribeWith(new BaseNetCallback<CommitOrderResultModel>() {

            @Override
            public void onSuccess(BaseModel<CommitOrderResultModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    commitOrderSuccessMutableLiveData.setValue(baseModel.getData());
                } else {
                    commitOrderErrorMutableLiveData.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                commitOrderErrorMutableLiveData.setValue(message);
            }
        }));
    }

    /**
     * 微信支付
     */
    public void getWeChatPay(String id, String json) {
        addSubscribe(mRetrofitHelper.getWeChatPay(id, "wechat", "yky", json).subscribeWith(new BaseNetCallback<WeChatInfoModel>() {

            @Override
            public void onSuccess(BaseModel<WeChatInfoModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    wechatPaySuccessMutableLiveData.setValue(baseModel.getData());
                } else {
                    wechatPayErrorMutableLiveData.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                wechatPayErrorMutableLiveData.setValue(message);
            }
        }));
    }

    /**
     * 支付宝
     */
    public void getAliPay(String id, String json) {
        addSubscribe(mRetrofitHelper.getAliPay(id, "ali", "yky", json).subscribeWith(new BaseNetCallback<AlipayModel>() {

            @Override
            public void onSuccess(BaseModel<AlipayModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    aliPaySuccessMutableLiveData.setValue(baseModel.getData());
                } else {
                    aliPayErrorMutableLiveData.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                aliPayErrorMutableLiveData.setValue(message);
            }
        }));
    }


    /**
     * 订单详情
     */
    public void getOrderDetails(String id) {
        addSubscribe(mRetrofitHelper.getOrderDetails(id).subscribeWith(new BaseNetCallback<OrderProductModel>() {

            @Override
            public void onSuccess(BaseModel<OrderProductModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode() && baseModel.getData() != null) {
                    orderDetailsMutableLiveData.setValue(baseModel.getData());
                } else {
                    ToastHelper.show(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                ToastHelper.show(message);
            }
        }));
    }

    /**
     * 查找我的企业
     */
    public void getMyCompanyList(int page, String keyword) {
        Map<String, Object> map = new HashMap<>();
        map.put("pageNum", page);
        map.put("pageSize", Constant.PAGE_SIZE);
        map.put("keyword", keyword);
        addSubscribe(mRetrofitHelper.getMyCompanyList(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<CompanyRecordsModel>() {

            @Override
            public void onSuccess(BaseModel<CompanyRecordsModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode() && baseModel.getData() != null) {
                    companyRecordsModelMutableLiveData.setValue(baseModel.getData());
                } else {
                    mErrorData.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                mErrorData.setValue(message);
            }
        }));
    }
}
